package com.atguigu.yygh.order.api;

import com.atguigu.yygh.enums.OrderStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.order.service.OrderService;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.util.AuthContextHolder;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import com.atguigu.yygh.vo.order.OrderQueryVo;
import com.atguigu.yygh.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zr
 * @date 2021/12/21 16:52
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Resource
    private OrderService orderService;

    @ApiOperation(value = "创建订单")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@ApiParam(name = "patientId", value = "就诊人id", required = true)
                            @PathVariable long patientId,
                            @ApiParam(name = "scheduleId", value = "排班id", required = true)
                            @PathVariable String scheduleId) {
        Long orderId = orderService.saveOrder(patientId, scheduleId);
        return Result.ok(orderId);
    }

    /**
     * 根据订单id查询订单详情
     */
    @ApiOperation("查询订单详情")
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrder(@PathVariable String orderId) {
        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderInfo);
    }

    @ApiOperation("订单条件查询带分页")
    @GetMapping("{page}/{limit}")
    public Result listOrders(@PathVariable long limit,
                             @PathVariable long page,
                             OrderQueryVo orderQueryVo, HttpServletRequest request) {
        // 设置当前的用户id 针对用户查询订单
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> orderInfoPage = new Page<>(page, limit);
        orderService.selectPage(orderInfoPage, orderQueryVo);
        return Result.ok(orderInfoPage);
    }

    @ApiOperation(value = "获取订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable("orderId") Long orderId) {
        Boolean flag = orderService.cancelOrder(orderId);
        return Result.ok(flag);
    }

    /**
     * 这里返回 map集合 用来给统计模块 实施远程调用
     *
     * @param orderCountQueryVo
     * @return
     */
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }

}
